package com.feilong.edgeAI.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

import androidx.annotation.RequiresApi;


/**
 * Email: 2185134304@qq.com
 * Created by Novate 2018/4/22 9:32
 * Version 1.0
 * Params:
 * Description:    守护进程，双进程通讯，需要使用aidl（进程间通讯）
 *                 两个进程之间相互监听，相互唤醒
*/
public class GuardService extends Service {
    private int GuardId = 1;


    /**
     * 返回 IBinder驱动
     * Stub: 存根
     */
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

//    private ProcessConnection mBinder = new ProcessConnection.Stub(){} ;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground();
        // 提高进程优先级 ，就会在通知栏中出现自己的应用，如果不想提高优先级，可以把这个注释
        startForeground(GuardId , new Notification());

        // 让GuardService绑定MessageService 并建立连接
        bindService(new Intent(this , FeiLongService.class) , mServiceConnection , Context.BIND_IMPORTANT) ;
        return START_STICKY;
    }

    private static final String TAG = "GuardService";
    private static final String CHANNEL_ID = "GuardServiceChannel";
    private static final int NOTIFICATION_ID = 103;
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startForeground() {
        // Create Notification Channel
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "GuardService",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }

        // Create Notification
        Notification notification = new Notification.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText("Service is running...")
                .build();

        // Start Foreground Service with notification
        startForeground(NOTIFICATION_ID, notification);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(mServiceConnection);
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 连接上
            Toast.makeText(GuardService.this , "建立连接" , Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 断开连接，就需要重新启动，然后重新绑定

            // 重新启动
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(new Intent(GuardService.this , FeiLongService.class)) ;
            } else {
                startService(new Intent(GuardService.this , FeiLongService.class)) ;
            }
            // 重新绑定
            bindService(new Intent(GuardService.this , FeiLongService.class) , mServiceConnection , Context.BIND_IMPORTANT) ;
        }
    } ;
}
